import { reactive, toRaw } from 'vue';
import type { CascaderOption } from 'vant/lib/cascader/types';
import type { SeriesItem } from '/@/api/gateruler/model/seriesModel';
import { FoilEnum, RarityEnum, AbilityEnum, LegionEnum, ClassEnum } from '/@/api/gateruler/model/cardModel';
import { SeriesListApi } from '/@/api/gateruler/series';
import { rulerType, rulerArray } from './rulerData';
import { uniqBy, filter, find } from 'lodash-es';

const foilOptions: CascaderOption[] = [
  { label: '平', value: FoilEnum.FLAT },
  { label: '闪', value: FoilEnum.FLASH },
];

const rarityOptions: CascaderOption[] = [
  { label: '无星', value: RarityEnum.NOSTAR },
  { label: '???', value: RarityEnum.QUESTION },
  { label: '1星', value: RarityEnum.STAR1 },
  { label: '2星', value: RarityEnum.STAR2 },
  { label: '3星', value: RarityEnum.STAR3 },
  { label: '4星', value: RarityEnum.STAR4 },
];

const abilityOptions: CascaderOption[] = [
  { label: AbilityEnum.CNT, value: AbilityEnum.CNT },
  { label: AbilityEnum.TD, value: AbilityEnum.TD },
  { label: AbilityEnum.OD, value: AbilityEnum.OD },
];

const categoryOptions: CascaderOption[] = [
  { label: '单位', value: '单位' },
  { label: '事件', value: '事件' },
  { label: '场地', value: '场地' },
  { label: '门将', value: '门将' },
];

const legionOptions: CascaderOption[] = [
  { label: 'atlas', value: LegionEnum.ATLAS },
  { label: 'wonderverse', value: LegionEnum.WONDERVERSE },
  { label: 'exvaders', value: LegionEnum.EXVADERS },
  { label: 'yomajinfront', value: LegionEnum.YOMAJINFRONT },
  { label: 'volnar', value: LegionEnum.VOLNAR },
];

const classOptions: CascaderOption[] = [
  { label: ClassEnum.S, value: ClassEnum.S },
  { label: ClassEnum.L, value: ClassEnum.L },
];

const levelOptions: CascaderOption[] = [
  { label: 4, value: 4 },
  { label: 3, value: 3 },
  { label: 2, value: 2 },
  { label: 1, value: 1 },
  { label: 0, value: 0 },
  { label: -2, value: -2 },
  { label: -5, value: -5 },
];

const attrsOptions = [
  '爱丽丝',
  '超凡者',
  '超自然',
  '地',
  '法师',
  '风',
  '光',
  '鬼之岛',
  '火力',
  '禁术',
  '军事',
  '科幻',
  '历史',
  '龙',
  '野蛮军',
  '魔',
  '叛徒',
  '商',
  '神',
  '事故',
  '天天搞事',
  '物体',
  '炎',
  '野兽',
  '宇宙民',
  '宇宙遗迹',
  '战士',
  'DWM',
  'OWL',
  '暗',
  '材质',
  '超级英雄',
  '机器人',
  '龙领主',
  '叛徒',
  '鲨鱼',
  '主要角色',
  '剑法',
];

const seriesOptions = () => {
  type YearSeries = {
    year: number;
    list: SeriesItem[];
  };
  const seriesData = reactive<SeriesItem[]>([]);
  const yearSeriesData = reactive<YearSeries[]>([]);
  const seriesAll = () => {
    SeriesListApi()
      .then((result) => {
        for (const item of result) {
          seriesData.push(item);
        }
        handleSeriesDate(toRaw(seriesData));
      })
      .catch(() => {});
  };
  seriesAll();
  const handleSeriesDate = (data: SeriesItem[]) => {
    yearSeriesData.length = 0;
    const yearArr = uniqBy(toRaw(data), 'publish_year');
    for (const year of yearArr) {
      const list = filter(toRaw(data), (item: SeriesItem) => {
        return year.publish_year === item.publish_year;
      });
      yearSeriesData.push({ year: year.publish_year, list: list });
    }
  };
  return {
    seriesData,
    yearSeriesData,
    handleSeriesDate,
  };
};

// 获取门将的组牌规则
const rulerGroupCard = (other_ename: string) => {
  const result = find(rulerArray, ['other_ename', other_ename]);
  if (!result) {
    const defaultResult: rulerType = {
      name: '',
      other_ename: '',
      maxLevel: -1,
      maxCNT: -1,
      maxLegion: -1,
      maxTotal: -1,
      maxSameCard: -1,
      maxSameLegendCard: -1,
      maxLegendTotal: -1,
    };
    return defaultResult;
  }
  return result;
};

export function useOptions() {
  return {
    foilOptions,
    rulerGroupCard,
    seriesOptions,
    classOptions,
    categoryOptions,
    rarityOptions,
    legionOptions,
    levelOptions,
    abilityOptions,
    attrsOptions,
  };
}
